package com.cans.web.pms;

import com.cans.dto.PageRes;
import com.cans.dto.ResDto;
import com.cans.model.pms.SysRole;
import com.cans.service.pms.api.ISysRoleService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author cans
 * @date 2018/5/1
 * @description 角色web层
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 路由跳转
     * @return
     */
    @GetMapping("/home")
    public String home(){

        return "role/roleList";
    }

    /**
     * 跳转视图
     * @return
     */
    @GetMapping("toAdd")
    public String toAdd(){

        return "role/roleAdd";
    }

    /**
     * 角色分页信息
     * @return
     */
    @GetMapping("/pageList")
    @ResponseBody
    public PageRes pageList( Integer page, Integer limit){

        Pageable pageable = new PageRequest(page - 1,limit);

        return sysRoleService.findPage(pageable);
    }

    /**
     * 新增
     * @param role
     * @return
     */
    @PostMapping(value = "add")
    @ResponseBody
    public ResDto add(SysRole role){

        try {
            sysRoleService.save(role);

        } catch (Exception e) {
            ResDto.fait(e.getMessage());
        }

        return ResDto.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    @ResponseBody
    public ResDto delete(@PathVariable String id){

        sysRoleService.delete(id);

        return ResDto.success();
    }
}
